// pages/find/find.js
function getRandomColor() {
    const rgb = []
    for (let i = 0; i < 3; ++i) {
      let color = Math.floor(Math.random() * 256).toString(16)
      color = color.length === 1 ? '0' + color : color
      rgb.push(color)
    }
    return '#' + rgb.join('')
  }
Page({

    /**
     * 页面的初始数据
     */
    data: {
        toggle:true,
        banner:[],
        poster:"http://47.104.209.44/base/imgs/1.jpg",
        name:"纤夫的爱",
        author:"杜拉拉",
        asrc:"https://mp3.haoge500.com/upload/128/2020/05/20/1004373.mp3",
        vSrc:"https://webstatic.mihoyo.com/upload/static-resource/2021/10/01/eab430cf5dd0c4bfc74a8901aeaee2fa_5484577763756444284.mp4",
        danmuList:
            [{
            text: '第 1s 出现的弹幕',
            color: '#ff0000',
            time: 1
            }, {
            text: '第 3s 出现的弹幕',
            color: '#ff00ff',
            time: 3
        }],
        text:"666",
        flag:true
    },
    videotimeupdate(){
        console.log("video - 播放ing")
        if(!wx.getStorageSync('isplay') && this.data.flag ){
            console.log("切换 wifi")
            this.setData({
                flag:false 
            })
            this.showModal()
        }
    },
    videoplay(){
        console.log("视频开始播放了。。。")
        if(!wx.getStorageSync('isplay')){
            this.showModal()
        }
    },
    showModal(){
        this.video.pause();
        wx.showModal({
            title:"流量提示",
            content:"你正在使用网络流量播放视频,是否继续?",
            cancelText:"取消观看",
            cancelColor:"#999",
            confirmText:"继续观看",
            confirmColor:"#f50",
            success:res=>{
                if(res.confirm){
                    this.video.play();
                    wx.setStorageSync('isplay', true)
                    wx.showToast({
                        title:"土豪,请继续",
                        icon:"none"
                    })
                }else{

                }
            }
        })
    },
    changeVideoSrc(){
        wx.chooseVideo({
            sourceType: ['album', 'camera'],
            maxDuration: 60,
            camera: ['front', 'back'],
            success:(res)=> {
              this.setData({
                vSrc: res.tempFilePath
              })
            }
        })
    },
    // 发送弹幕
    sendDanmu(){
        this.video.sendDanmu({
            text: this.data.text,
            color: getRandomColor()
        })
        this.setData({
            text:'666'
        })
    },
    audioplay(){
        this.audioCtx.play()
    },
    audiopause(){
        this.audioCtx.pause()
    },
    changeView(){
        this.setData({
            toggle:false
        })
    },

    getBanners(){
        wx.showLoading({title:"请求中"})
        wx.request({
            url:"https://m.maizuo.com/gateway?type=2&cityId=440300&k=6250284",
            method:"get",
            data:{},
            header:{
                'X-Host': 'mall.cfg.common-banner',
               'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.0.4","e":"16364508084833332856619009"}'
            },
            success:res=>{
                console.log(res)
                wx.hideLoading()
                wx.showToast({
                    title:"请求成功",
                    icon:'none'
                })
                this.setData({
                    banner:res.data.data
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getBanners()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // 使用 wx.createAudioContext 获取 audio 上下文 context
        this.audioCtx = wx.createAudioContext('myAudio')

        this.video = wx.createVideoContext('myVideo')

        // 判断网络状态 JSSDk
        wx.getNetworkType({
            success (res) {
              const networkType = res.networkType
              console.log(networkType)
              wx.setStorageSync('isplay', networkType=="wifi")
            }
        })

        // 监听网络状态改变
        wx.onNetworkStatusChange(function (res) {
            console.log(res.isConnected)
            console.log(res.networkType)
            wx.setStorageSync('isplay', res.networkType=="wifi")
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})